//
//  LDMicroCompanyPage.swift
//  DingDongZP
//
//  Created by 三秒文化 on 16/7/13.
//  Copyright © 2016年 51ledian.com. All rights reserved.
//

import UIKit

@objc(LDMicroCompanyPage)
class LDMicroCompanyPage: LDBaseCustomizeVC {

    var tableView:LDBaseTableView?
    var tableHeadView:LDMicroCompanyHeadView?
    var model:DDCompanyInfo?
    var cellHArr = [CGFloat]()
    
    var cellHForCompanyIntro = CGFloat()
    var cellDescLabLimitH:CGFloat = 90
    
    var cellCompanyDescLabStableH:CGFloat = 90
    var cellDescLabStableH:CGFloat = 145
    
    
    lazy var headerViewTransImg:UIImage = {
        
        let image = UIImage.setImageWithColor(LDGolbalColor.Green.colorWithAlphaComponent(0))
        return image
    }()
    
    lazy var shareView:DDShareView = {
        
        let lazyview:DDShareView = NSBundle.mainBundle()
            .loadNibNamed("DDShareView", owner: self, options: nil).first as? DDShareView ?? DDShareView()
        lazyview.userInteractionEnabled = true
        
        return lazyview
    }()
    
    override func viewDidLoad() {
        
//        super.viewDidLoad()
        setNav()
        setDefaultContent()
        requestNet()
    }
    
    override func viewWillAppear(animated: Bool) {
        rdv_tabBarController.setTabBarHidden(true, animated: true)
    }
    
    func shareClick() {
        
        let shareModel = DDShareModel();
        
        if model == nil {
            
            view.makeToast("分享失败")
            return
        }
        
        shareModel.shareH5 = DDAPIGenerate.h5URLWithPath(model!.shareh5 ?? "")
        shareModel.shareLogoUrl = DDAPIGenerate.imageURLWithPath(model!.company_logo ?? "")
        shareModel.shareDesc = DDAPIGenerate.imageURLWithPath(model!.sharedesc ?? "")
      
        shareModel.shareTitle_WeiXin = model!.sharetitle_weixin;
        shareModel.shareTitle_WeiBo = model!.sharetitle_weibo;
        shareModel.shareTitle_SMS = model!.sharetitle_sms;
       
        self.shareView.shareModel = shareModel;
        
        self.shareView.show()
        
    }
    
    func requestNet() {
        
        //公司信息
        LDBossInfoNet.sharedInstance.queryCompanyInfo({ [weak self] (dict) in
            
            self?.model = DDCompanyInfo.yy_modelWithDictionary(dict as [NSObject : AnyObject])
            
            NSOperationQueue.mainQueue().addOperationWithBlock({ 
                
                self?.tableHeadView?.model = self?.model
                
                self?.getCompanyIntroCellHeigthFromModel()
                self?.getCellHeigthFromModel()
                self?.tableView?.reloadData()
            })
            }, failure: nil)
    }
    
    func setNav() {
        
        let shareBarItem = UIBarButtonItem(image: UIImage(named:"nav_share_white"), style: .Done, target: self, action: #selector(self.shareClick))

        navigationItem.rightBarButtonItem = shareBarItem
        rdv_tabBarController.setTabBarHidden(false, animated: true)
    }
    
    func setDefaultContent(){
        
        view.backgroundColor = LDGolbalColor.BgGoundGray
        tableView = LDBaseTableView()
        
        tableView?.delegate = self
        tableView?.dataSource = self
        tableView?.separatorStyle = .None
        tableView?.rowHeight = UITableViewAutomaticDimension
        
        tableHeadView = LDMicroCompanyHeadView()
        tableHeadView?.frame = CGRectMake(0, 0,LDFrame.ScreenWidth, 340)
        
        tableView?.tableHeaderView = tableHeadView
        view.addSubview(tableView!)
        
        tableView?.snp_makeConstraints(closure: { (make) in
//            make.top.equalTo(-64)
            make.top.left.bottom.right.equalTo(0)
        })
        
    }
}

extension LDMicroCompanyPage:UITableViewDelegate,UITableViewDataSource {

    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int
    {
    
      return 2
    }
    
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if section == 0 {
            
            return 1
        }else{

        return model?.jobs.count ?? 0
        }
    }
    
    
    func  tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        
        if indexPath.section == 0{
            
            var cell = tableView.dequeueReusableCellWithIdentifier("DDLDCompanyIntroduceCell") as?DDLDCompanyIntroduceCell
            
            if cell == nil {
                
                cell = DDLDCompanyIntroduceCell(style: .Default, reuseIdentifier: "DDLDCompanyIntroduceCell")
            }
            cell?.tag = indexPath.row
            cell?.tapMoreIntroduceBlock = { [weak self] selCell in
                
                self?.getCompanyIntroCellHeigthRefresh(selCell.detailsBtn.selected)

                NSOperationQueue.mainQueue().addOperationWithBlock({ [weak self] in
                    
                    self?.tableView?.reloadData()
                    })
            }
            
            if model != nil {
                
                let cellmodel = model
                cellmodel?.companyDescH = cellHForCompanyIntro - cellCompanyDescLabStableH
                cell?.models = cellmodel!
            }
            
            return cell!
            
        }
        
        if indexPath.section == 1 {
            
            
            var cell = tableView.dequeueReusableCellWithIdentifier("LDMicroCompanyCellTableViewCell") as?LDMicroCompanyCellTableViewCell
            
            if cell == nil {
                
                cell = LDMicroCompanyCellTableViewCell(style: .Default, reuseIdentifier: "LDMicroCompanyCellTableViewCell")
            }
            cell?.tag = indexPath.row
            cell?.tapMoreDescBlock = { [weak self] selCell in
                
                self?.getCellHeigthRefresh(selCell.tag, sel: selCell.detailsBtn.selected)
                NSOperationQueue.mainQueue().addOperationWithBlock({ [weak self] in
                    
                    self?.tableView?.reloadData()
                    })
            }
            
            if model != nil {
                
                let cellmodel = model!.jobs[indexPath.row]
                cellmodel.jobDescH = cellHArr[indexPath.row] - cellDescLabStableH
                cell?.models = cellmodel
            }
            
            return cell!
            
        }
        
        var cellnil = tableView.dequeueReusableCellWithIdentifier("LDMicroCompanyCellTableViewCell") as?LDMicroCompanyCellTableViewCell
        
        return cellnil!
    
    }
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        
        if indexPath.section == 0 {
            
            return cellHForCompanyIntro
        }else{
            
        return cellHArr[indexPath.row]
        }
    }
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
//        let offSetY = self.tableView!.contentOffset.y
        
//        if offSetY < 0 {
//            
////            navigationController?.setNavigationBarHidden(true, animated: true)
//            navigationController?.navigationBar.hidden = true
//            title = ""

            
//        } else {
            
            title = "我的微主页"
            navigationController?.navigationBar.hidden = false
            
            navigationController?.navigationBar.barTintColor = LDGolbalColor.Green
            navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName : LDFont.Font16,NSForegroundColorAttributeName:LDGolbalColor.White]
//        }
        
    }
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        
        if indexPath.section == 1 {
            
            let vc = DDBossWorkDetailViewController()
            vc.jobId = model?.jobs[indexPath.row].jobid
            navigationController?.pushViewController(vc, animated: false)
        }
    }
    
    
    func getCompanyIntroCellHeigthFromModel() {
        
        
        
            var defaultH = String.getHeightFormAttr(8, title: model?.company_intro ?? "公司介绍", size: CGSizeMake(LDFrame.ScreenWidth - LDMargin_V*10, 8888), font: LDFont.Font12) + LDMargin_V
            
            defaultH = defaultH > cellDescLabLimitH ? cellDescLabLimitH : defaultH
            
            cellHForCompanyIntro = defaultH + cellCompanyDescLabStableH
    }
    
    
    
    func getCellHeigthFromModel() {
        
        
        for cellModel in model!.jobs {
            
            var defaultH = String.getHeightFormAttr(8, title: cellModel.jobdesc ?? "职位简介", size: CGSizeMake(LDFrame.ScreenWidth - LDMargin_V*10, 8888), font: LDFont.Font12) + LDMargin_V
            
            defaultH = defaultH > cellDescLabLimitH ? cellDescLabLimitH : defaultH
            
            let cellH = defaultH + cellDescLabStableH
            cellHArr.append(cellH)
        }
    }
    
    
    func getCompanyIntroCellHeigthRefresh(sel:Bool) {
        
        
        let defaultTextH = String.getHeightFormAttr(8, title: model?.company_intro ?? "公司介绍", size: CGSizeMake(LDFrame.ScreenWidth - LDMargin_V*10, 8888), font: LDFont.Font12) + Margin_V
        
        
        let trueLabH = defaultTextH + cellCompanyDescLabStableH
        let limitLabH:CGFloat = cellDescLabLimitH + cellCompanyDescLabStableH
        
        if sel == true && defaultTextH > cellDescLabLimitH {
            
            cellHForCompanyIntro = trueLabH
        } else {
            
            if defaultTextH > cellDescLabLimitH{
                
                cellHForCompanyIntro = limitLabH
            } else {
                
                cellHForCompanyIntro = defaultTextH + cellCompanyDescLabStableH
            }
            
        }
    }

    func getCellHeigthRefresh(row:Int,sel:Bool) {
        
        let selModel = model!.jobs[row]
        
        let defaultTextH = String.getHeightFormAttr(8, title: selModel.jobdesc ?? "职位简介", size: CGSizeMake(LDFrame.ScreenWidth - LDMargin_V*10, 8888), font: LDFont.Font12) + Margin_V


        let trueLabH = defaultTextH + cellDescLabStableH
        let limitLabH:CGFloat = cellDescLabLimitH + cellDescLabStableH
        
        if sel == true && defaultTextH > cellDescLabLimitH {
            
            cellHArr[row] = trueLabH
        } else {
            
            if defaultTextH > cellDescLabLimitH{
                
                cellHArr[row] = limitLabH
            } else {
                cellHArr[row] = defaultTextH + cellDescLabStableH
            }
            
        }
    }
}